package com.lanhao.aot.carrierintegration.gwe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.lanhao.aot.carrierintegration.AeIfRequestDTO;
import com.lanhao.aot.carrierintegration.Warehouse;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;

/**
 * 瑞士物流商对接 - 安和直接对接
 * @author Administrator
 */
public class GweCHDPDUtil {
	private static final Logger LOG = LoggerFactory.getLogger(GweCHDPDUtil.class);

	// 正式环境
	public static final String URL = "https://api.gfsbiz.com.cn/LabelTemplate";
	public static final String USER_NAME = "ANHE";
	public static final String USER_PWD = "5db6d88ed221cda5a9ff13c03226b551";
	public static final String CHANNEL_NUMBER = "CHDPDRETURN_J";
	
	// 测试环境
//	public static final String URL = "http://api.gfsbiz.com.cn/LabelTemplate";
//	public static final String USER_NAME = "ANHE";
//	public static final String USER_PWD = "5db6d88ed221cda5a9ff13c03226b551";
//	public static final String CHANNEL_NUMBER = "GWE_TEST";
	
	public static final String ORDER_URL = "/GetLabelALLAPI";
	public static final String COUNTRY_CODE = "CH";
	public static final Double DEFAULT_WEIGHT = 0.14d;
	public static final String Carrier = "DPD(CH)"; // 100815

	public static void main(String[] args) {
		
//		姓名： LAM HOANG CAM
//		街名： Av C.-F RAMUZ 29
//		城市及邮编： 1009 Pully
//		国家： SWITZERLAND
		
		String countryCode = COUNTRY_CODE;
		AeIfRequestDTO request = new AeIfRequestDTO();
		request.setCountry_code(countryCode);
		request.setShipper_country_code(countryCode);
		Warehouse warehouse = new Warehouse();
		warehouse.setNationCode(countryCode);
		
		request.setShipper_city("Pully");
		request.setShipper_name("KunKun");
		request.setShipper_phone("41794296810");
		request.setWarehouse_code("CK" + countryCode + "000001");
		request.setShipper_post_code("1009");
		request.setShipper_province("Vaud");
		request.setShipper_street("Av C.-F RAMUZ 30");
		
		warehouse.setCity("Pully");
		warehouse.setPhone("41794296810");
		warehouse.setPostCode("1009");
		warehouse.setReceiver("CH-RTN-AE");
		warehouse.setState("Vaud");
		warehouse.setStreet("Av C.-F RAMUZ 29");
		warehouse.setWarehouseCode("CK" + countryCode + "000001");
		// 结束调用瑞士标签，返回结果respBody={"PrintInfoALL":[{"State":0,"Code":"GWE_TEST000000504","ErrCode":null,"Url":"http://gwelabel.gw-express.com.cn/GWE_TEST000000504_20231020153235231174232","Msg":null,"CustomerCode":"87b859eea4aa4d2a911c3ee7b4ff10b2"}],"Url":"","State":"0","Error":null}
		createOrder(request, warehouse, UUID.fastUUID().toString(true));
	}
	
	public static void createOrder(AeIfRequestDTO afRequest, Warehouse warehouse, String taskNo) {
		String orderUrl = URL + ORDER_URL;
		JSONObject request = new JSONObject();
		request.put("UserName", USER_NAME);
		request.put("UserPWD", USER_PWD);
		request.put("ChannelNumber", CHANNEL_NUMBER);
		request.put("ChannelType", "1");
		JSONArray labelParameterALL = new JSONArray();
		JSONObject labelParameter = new JSONObject();
		JSONObject ordersModel = new JSONObject();
		ordersModel.put("ReceiveCountry", COUNTRY_CODE);
		ordersModel.put("CustomerCode", taskNo);
		ordersModel.put("Weight", DEFAULT_WEIGHT);
		ordersModel.put("Total", 1);
		ordersModel.put("Type", 2); // Cargo data type (0 file 1 attachment 2 package)
//		ordersModel.put("IsCod", 0);
		ordersModel.put("CodAmount", 0);
        // 收件人信息
//		ordersModel.put("CompanyName", "AnHe Com.");
		ordersModel.put("ReceiveName", warehouse.getReceiver());
		ordersModel.put("City", warehouse.getCity());
		ordersModel.put("Zip", warehouse.getPostCode());
		ordersModel.put("Address1", warehouse.getStreet());
//		ordersModel.put("province", "");
//		ordersModel.put("Phone", "");
//		ordersModel.put("Email", "");
//		ordersModel.put("Mobile", "");
//		ordersModel.put("Address2", null);
//		ordersModel.put("Address3", null);
		ordersModel.put("DeclareType", 3); // Type of package declaration (0 gift 1 business sample 2 document 3 others)
		labelParameter.put("OrdersModel", ordersModel);
		// 发件人信息
		JSONObject sendersModel = new JSONObject();
//		sendersModel.put("CompanyName", "FF");
		sendersModel.put("SenderName", afRequest.getShipper_name());
		sendersModel.put("Country", afRequest.getCountry_code());
//		sendersModel.put("Province", "");
		sendersModel.put("City", afRequest.getShipper_city());
		sendersModel.put("Address", afRequest.getShipper_street());
//		sendersModel.put("DoorNum", "");
//		sendersModel.put("Email", "");
		sendersModel.put("Zip", afRequest.getShipper_post_code());
		sendersModel.put("Mobile", afRequest.getShipper_phone());
		labelParameter.put("SendersModel", sendersModel);

//		JSONArray declarationsAll = new JSONArray();
//		JSONObject declarations = new JSONObject();
//		declarations.put("EnglishName", "1");
//		declarations.put("ChineseName", "1");
//		declarations.put("Total", 3);
//		declarations.put("Unit", "USD");
//		declarations.put("Amount", 5.94);
//		declarations.put("TotalAmount", 17.83);
//		declarations.put("Weight", DEFAULT_WEIGHT);
//		declarationsAll.add(declarations);

//		labelParameter.put("DeclarationsAll", declarationsAll);

		labelParameterALL.add(labelParameter);
		request.put("LabelParameterALL", labelParameterALL);
		LOG.info("[{}]开始请求Gwe瑞士标签，请求url={}，参数request={}", taskNo, orderUrl, request.toJSONString());
		String respBody = HttpRequest.post(orderUrl).body(request.toJSONString()).execute().body();
		LOG.info("[{}]结束调用Gwe瑞士标签，返回结果respBody={}", taskNo, respBody);
		if (StringUtils.isNotBlank(respBody)) {
			JSONObject respObject = JSON.parseObject(respBody);
			int state = respObject.getIntValue("State");
			String error = respObject.getString("Error");
			if (state == 0) {
				JSONArray printInfoALLArray = respObject.getJSONArray("PrintInfoALL");
				if (printInfoALLArray != null && printInfoALLArray.size() > 0) {
					JSONObject printInfoObject = printInfoALLArray.getJSONObject(0);
					int printState = printInfoObject.getIntValue("State");
					String errMsg = printInfoObject.getString("Msg");
					String errCode = printInfoObject.getString("ErrCode");
					if (printState == 0) {
						String trackNumber = printInfoObject.getString("Code");
						String labelUrl = printInfoObject.getString("Url");
						LOG.info("请求标签成功，trackNumber={}，labelUrl={}", trackNumber, labelUrl);
					} else {
						LOG.info("请求标签失败，errCode={}，errMsg={}", errCode, errMsg);
					}
				}
			} else {
				LOG.info("获取标签失败，state={}， error={}", state, error);
			}
		}
	}
}
